package Util;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

/**
 * Created by tan on 2017/8/25.
 * 15:09
 */
public class FileUtil {


    private  MultipartFile file;

    private  HttpServletRequest request;

    public FileUtil(MultipartFile file,HttpServletRequest request)
    {
        this.file=file;
        this.request=request;
    }
    public FileUtil(HttpServletRequest request)
    {
        this.request=request;
    }

    public  String uploadVideo()
    {

        try {
            if (this.file.isEmpty()) {
                System.out.println("文件未上传");
            } else {
                System.out.println("文件长度: " + file.getSize());
                System.out.println("文件类型: " + file.getContentType());
                System.out.println("文件名称: " + file.getName());
                System.out.println("文件原名: " + file.getOriginalFilename());
                String name=file.getOriginalFilename();

                String realPath=request.getSession().getServletContext().getRealPath("/upload/video");
                String id= UUID.randomUUID().toString().replace("-","");
                FileUtils.copyInputStreamToFile(file.getInputStream(), new File(realPath,id+name));

                return "/upload/video/"+id+name;
            }
        }catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
        return null;
    }
    public  String uploadImg(String base64,String pathname)
    {
        if (base64 == null) //图像数据为空
            return "error";
        BASE64Decoder decoder = new BASE64Decoder();
        try
        {
            //Base64解码
            byte[] b = decoder.decodeBuffer(base64);
            for(int i=0;i<b.length;++i)
            {
                if(b[i]<0)
                {//调整异常数据
                    b[i]+=256;
                }
            }

            //生成jpeg图片
            String imgFilePath=request.getSession().getServletContext().getRealPath("/upload/img");//新生成的图片
            System.out.println(imgFilePath);
            FileUtils.copyInputStreamToFile(new ByteArrayInputStream(b), new File(imgFilePath,pathname+".jpg"));

//            OutputStream out = new FileOutputStream(imgFilePath);
//            out.write(b);
//            out.flush();
//            out.close();
            return "\\upload\\img\\"+pathname+".jpg";
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return "error";
        }
    }


    public String getName(){
        return file.getOriginalFilename();
    }

    public String getPath()
    {
        StringBuilder path = new StringBuilder();

        path.append(request.getSession().getServletContext().getRealPath("/upload/video"));
        path.append("/");
        path.append(file.getOriginalFilename());
        return  path.toString();
    }
}
